﻿using System;
using System.Collections;
using System.Collections.Generic;
using EnterpriseLibraryWrapper;
using itau.im.dado;
using itau.im.entidade;
using Itau.IM.CAS;

namespace itau.im.negocio
{
    public class PricingBS : IPricingBS
    {
        public List<Serie> ObterSeriesPorCodigoFamilia(int codigoFamilia)
        {
            IPricingDAO pricingDAO;

            try
            {
                pricingDAO = DaoFactory.getPricingDAO();

                return pricingDAO.ObterSeriesPorCodigoFamilia(codigoFamilia);
            }
            catch (DadosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (NegociosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (Exception ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            finally
            {
                pricingDAO = null;
            }

            return null;
        }

        public string ObterValorSerieAtributo(int codigoSerieIntegrante, int codigoAtributoIntegrante, DateTime dataHoraExecucao)
        {
            IPricingDAO pricingDAO;
            ComposicaoCurva composicaoCurva = new ComposicaoCurva();
            IList<ItemComposicao> listaItens = new List<ItemComposicao>();
            int codigoFamilia = 0;

            try
            {
                pricingDAO = DaoFactory.getPricingDAO();

                if (codigoSerieIntegrante.ToString().Length > 4) 
                {
                    codigoFamilia = Convert.ToInt32(codigoSerieIntegrante.ToString().Substring(0, 4));
                }

                return pricingDAO.ObterValorSerieAtributo(codigoFamilia, codigoSerieIntegrante, codigoAtributoIntegrante, 2, dataHoraExecucao);
            }
            catch (DadosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (NegociosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (Exception ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            finally
            {
                pricingDAO = null;
            }

            return string.Empty;
        }

        public List<Serie> ObterSeriesPorNomeSerie(string nomeSerie)
        {
            IPricingDAO pricingDAO;

            try
            {
                pricingDAO = DaoFactory.getPricingDAO();

                return pricingDAO.ObterSeriesPorNomeSerie(nomeSerie);
            }
            catch (DadosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (NegociosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (Exception ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            finally
            {
                pricingDAO = null;
            }

            return null;
        }

        public List<Familia> ObterFamiliaPorCodigoFamilia(int codigoFamilia)
        {
            IPricingDAO pricingDAO;

            try
            {
                pricingDAO = DaoFactory.getPricingDAO();

                return pricingDAO.ObterFamiliaPorCodigoFamilia(codigoFamilia);
            }
            catch (DadosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (NegociosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (Exception ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            finally
            {
                pricingDAO = null;
            }

            return null;
        }

        public List<Familia> ObterFamiliaPorNomeFamilia(string nomeFamilia)
        {
            IPricingDAO pricingDAO;

            try
            {
                pricingDAO = DaoFactory.getPricingDAO();

                return pricingDAO.ObterFamiliaPorNomeFamilia(nomeFamilia);
            }
            catch (DadosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (NegociosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (Exception ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            finally
            {
                pricingDAO = null;
            }

            return null;
        }

        public List<Atributo> ObterAtributosPorCodigoFamiliaCodigoTipoAtributo(int codigoFamilia, Int16 codigoTipoAtributo, Int16 codigoTipoDadoAtributo)
        {
            IPricingDAO pricingDAO;

            try
            {
                pricingDAO = DaoFactory.getPricingDAO();

                return pricingDAO.ObterAtributosPorCodigoFamiliaCodigoTipoAtributo(codigoFamilia, codigoTipoAtributo, codigoTipoDadoAtributo);
            }
            catch (DadosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (NegociosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (Exception ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            finally
            {
                pricingDAO = null;
            }

            return null;
        }

        public List<Serie> ObterSeriesPorCodigoSerie(int codigoSerie)
        {
            IPricingDAO pricingDAO;

            try
            {
                pricingDAO = DaoFactory.getPricingDAO();

                return pricingDAO.ObterSeriesPorCodigoSerie(codigoSerie);
            }
            catch (DadosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (NegociosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (Exception ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            finally
            {
                pricingDAO = null;
            }

            return null;
        }

        public List<Familia> ObterFamilias()
        {
            IPricingDAO pricingDAO;

            try
            {
                pricingDAO = DaoFactory.getPricingDAO();

                return pricingDAO.ObterFamilias();
            }
            catch (DadosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (NegociosException ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            catch (Exception ex)
            {
                EnterpriseLibraryFactory.GetExceptionHandler().HandleException(ex, "ErrosNegocios");
            }
            finally
            {
                pricingDAO = null;
            }

            return null;
        }
    }
}
